package sales.seesion.stateless;

import java.text.DecimalFormat;
import sales.entity.Product;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import sales.entity.LineItem;
import sales.entity.SalesOrderEntity;
import sales.util.exception.IllegalCompanyTypeException;
import sales.util.exception.PriceUnacceptableException;
import sales.util.exception.SalesException;

@Stateless
@LocalBean
public class PriceManageSessionBean {

    @PersistenceContext
    private EntityManager em;
    public double unitPrice = 0.0;
    private final int DS_LEVEL_1_QUANTITY = 5;
    private final int DS_LEVEL_2_QUANTITY = 10;
    private final int WS_LEVEL_1_QUANTITY = 20;
    private final int WS_LEVEL_2_QUANTITY = 100;
    private final double DS_INITIAL_DISCOUNT = 1.0;
    private final double DS_LEVEL_1_DISCOUNT = 0.95;
    private final double DS_LEVEL_2_DISCOUNT = 0.9;
    private final double WS_INITIAL_DISCOUNT = 0.9;
    private final double WS_LEVEL_1_DISCOUNT = 0.9;
    private final double WS_LEVEL_2_DISCOUNT = 0.8;
    private final String DS_COMPANY_TYPE = "Direct Sales";
    private final String WS_COMPANY_TYPE = "Whole Sales";

//    public double calculatePrice(String companyTypeCode, Product product, int quantity) {
//        double price = 0;
//
//        if (product.getProductName().equals("Classic Nutri Bar-Nuts")) { //classic-nuts
//            price = calculatePirceForA(companyTypeCode, product, quantity);
//        } else if (product.getProductName().equals("Classic Nutri Bar-Fruits")) { //classic-fruit
//            price = calculatePirceForB(companyTypeCode, product, quantity);
//        } else if (product.getProductName().equals("Premium Power Bar-Chocolate Nuts")) { //premium-nuts
//            price = calculatePirceForC(companyTypeCode, product, quantity);
//        } else { //other: premium-fruit
//            price = calculatePirceForD(companyTypeCode, product, quantity);
//        }
//        return price;
//    }
    public double calculatePirce(String companyType, Product product, int qty) throws SalesException {
        if (product.getPromotionPrice() == 0) {
            unitPrice = product.getBasePrice();
        } else {
            unitPrice = product.getPromotionPrice();
        }

        if (companyType.equals(DS_COMPANY_TYPE)) {  //companyTypeCode DS for Direct Sales
            if (qty > DS_LEVEL_1_QUANTITY && qty <= DS_LEVEL_2_QUANTITY) {
                unitPrice = unitPrice * DS_LEVEL_1_DISCOUNT;
            } else if (qty > DS_LEVEL_2_QUANTITY) {
                unitPrice = unitPrice * DS_LEVEL_2_DISCOUNT;
            }
        } else if (companyType.equals(WS_COMPANY_TYPE)) { //companyTypeCode WS for Whole Sales
            unitPrice = unitPrice * WS_INITIAL_DISCOUNT;
            if (qty > WS_LEVEL_1_QUANTITY && qty <= WS_LEVEL_2_QUANTITY) {
                unitPrice = unitPrice * WS_LEVEL_1_DISCOUNT;
            } else if (qty > WS_LEVEL_2_QUANTITY) {
                unitPrice = unitPrice * WS_LEVEL_2_DISCOUNT;
            }
        } else //invalid companyTypeCode
        {
            throw new IllegalCompanyTypeException();
        }


        return roundTwoDecimals(unitPrice);

    }

    public void verifySalesOrderPrice(SalesOrderEntity salesOrder) throws SalesException {

        int qty;
        double price;
        if (salesOrder.getCustomer().getCompanyType().equals(DS_COMPANY_TYPE)) {

            for (LineItem lineItem : salesOrder.getSalesOrderLineItems()) {

                if (lineItem.getProduct().getPromotionPrice() == 0) {

                    unitPrice = lineItem.getProduct().getBasePrice();
                } else {

                    unitPrice = lineItem.getProduct().getPromotionPrice();
                }
                qty = lineItem.getQuantity();
                price = lineItem.getPrice();

                if (((qty < DS_LEVEL_1_QUANTITY) && (price < unitPrice * DS_INITIAL_DISCOUNT))
                        || ((qty < DS_LEVEL_2_QUANTITY) && (price < unitPrice * DS_LEVEL_1_DISCOUNT * DS_INITIAL_DISCOUNT))
                        || (price < unitPrice * DS_LEVEL_2_DISCOUNT * DS_INITIAL_DISCOUNT)) {
                    throw new PriceUnacceptableException();
                }

            }
            return;
        } else if (salesOrder.getCustomer().getCompanyType().equals(WS_COMPANY_TYPE)) {

            for (LineItem lineItem : salesOrder.getSalesOrderLineItems()) {

                if (lineItem.getProduct().getPromotionPrice() == 0) {

                    unitPrice = lineItem.getProduct().getBasePrice();
                } else {

                    unitPrice = lineItem.getProduct().getPromotionPrice();
                }
                qty = lineItem.getQuantity();
                price = lineItem.getPrice();

                if (((qty < WS_LEVEL_1_QUANTITY) && (price < unitPrice * WS_INITIAL_DISCOUNT))
                        || ((qty < WS_LEVEL_2_QUANTITY) && (price < unitPrice * WS_LEVEL_1_DISCOUNT * WS_INITIAL_DISCOUNT))
                        || (price < unitPrice * WS_LEVEL_2_DISCOUNT * WS_INITIAL_DISCOUNT)) {
                    throw new PriceUnacceptableException();
                }

            }
            return;
        } else {
            throw new IllegalCompanyTypeException();
        }

    }

    public double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
}
